home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / fortran / f2c-9510.000 / f2c-9510 / f2c-951007-libs-1.1 / src / malloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-07  |  3.5 KB  |  167 lines

  1. /****************************************************************
  2. Copyright 1990, 1994 by AT&T Bell Laboratories and Bellcore.
  3.  
  4. Permission to use, copy, modify, and distribute this software
  5. and its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the names of AT&T Bell Laboratories or
  10. Bellcore or any of their entities not be used in advertising or
  11. publicity pertaining to distribution of the software without
  12. specific, written prior permission.
  13.  
  14. AT&T and Bellcore disclaim all warranties with regard to this
  15. software, including all implied warranties of merchantability
  16. and fitness.  In no event shall AT&T or Bellcore be liable for
  17. any special, indirect or consequential damages or any damages
  18. whatsoever resulting from loss of use, data or profits, whether
  19. in an action of contract, negligence or other tortious action,
  20. arising out of or in connection with the use or performance of
  21. this software.
  22. ****************************************************************/
  23.  
  24. #ifndef CRAY
  25. #define STACKMIN 512
  26. #define MINBLK (2*sizeof(struct mem) + 16)
  27. #define F _malloc_free_
  28. #define SBGULP 8192
  29. #include "string.h"    /* for memcpy */
  30.  
  31. #ifdef KR_headers
  32. #define Char char
  33. #define Unsigned unsigned
  34. #define Int /*int*/
  35. #else
  36. #define Char void
  37. #define Unsigned size_t
  38. #define Int int
  39. #endif
  40.  
  41. typedef struct mem {
  42.     struct mem *next;
  43.     Unsigned len;
  44.     } mem;
  45.  
  46. mem *F;
  47.  
  48.  Char *
  49. #ifdef KR_headers
  50. malloc(size)
  51.     register Unsigned size;
  52. #else
  53. malloc(register Unsigned size)
  54. #endif
  55. {
  56.     register mem *p, *q, *r, *s;
  57.     unsigned register k, m;
  58.     extern Char *sbrk(Int);
  59.     char *top, *top1;
  60.  
  61.     size = (size+7) & ~7;
  62.     r = (mem *) &F;
  63.     for (p = F, q = 0; p; r = p, p = p->next) {
  64.         if ((k = p->len) >= size && (!q || m > k)) {
  65.             m = k;
  66.             q = p;
  67.             s = r;
  68.             }
  69.         }
  70.     if (q) {
  71.         if (q->len - size >= MINBLK) { /* split block */
  72.             p = (mem *) (((char *) (q+1)) + size);
  73.             p->next = q->next;
  74.             p->len = q->len - size - sizeof(mem);
  75.             s->next = p;
  76.             q->len = size;
  77.             }
  78.         else
  79.             s->next = q->next;
  80.         }
  81.     else {
  82.         top = (Char *)(((long)sbrk(0) + 7) & ~7);
  83.         if (F && (char *)(F+1) + F->len == top) {
  84.             q = F;
  85.             F = F->next;
  86.             }
  87.         else
  88.             q = (mem *) top;
  89.         top1 = (char *)(q+1) + size;
  90.         if (sbrk((int)(top1-top+SBGULP)) == (Char *) -1)
  91.             return 0;
  92.         r = (mem *)top1;
  93.         r->len = SBGULP - sizeof(mem);
  94.         r->next = F;
  95.         F = r;
  96.         top1 += SBGULP;
  97.         q->len = size;
  98.         }
  99.     return (Char *) (q+1);
  100.     }
  101.  
  102.  void
  103. #ifdef KR_headers
  104. free(f)
  105.     Char *f;
  106. #else
  107. free(Char *f)
  108. #endif
  109. {
  110.     mem *p, *q, *r;
  111.     char *pn, *qn;
  112.  
  113.     if (!f) return;
  114.     q = (mem *) ((char *)f - sizeof(mem));
  115.     qn = (char *)f + q->len;
  116.     for (p = F, r = (mem *) &F; ; r = p, p = p->next) {
  117.         if (qn == (Char *) p) {
  118.             q->len += p->len + sizeof(mem);
  119.             p = p->next;
  120.             }
  121.         pn = p ? ((char *) (p+1)) + p->len : 0;
  122.         if (pn == (Char *) q) {
  123.             p->len += sizeof(mem) + q->len;
  124.             q->len = 0;
  125.             q->next = p;
  126.             r->next = p;
  127.             break;
  128.             }
  129.         if (pn < (char *) q) {
  130.             r->next = q;
  131.             q->next = p;
  132.             break;
  133.             }
  134.         }
  135.     }
  136.  
  137.  Char *
  138. #ifdef KR_headers
  139. realloc(f, size)
  140.     Char *f;
  141.     Unsigned size;
  142. #else
  143. realloc(Char *f, Unsigned size)
  144. #endif
  145. {
  146.     mem *p;
  147.     Char *q, *f1;
  148.     Unsigned s1;
  149.  
  150.     if (!f) return malloc(size);
  151.     p = (mem *) ((char *)f - sizeof(mem));
  152.     s1 = p->len;
  153.     free(f);
  154.     if (s1 > size)
  155.         s1 = size + 7 & ~7;
  156.     if (!p->len) {
  157.         f1 = (Char *)(p->next + 1);
  158.         memcpy(f1, f, s1);
  159.         f = f1;
  160.         }
  161.     q = malloc(size);
  162.     if (q && q != f)
  163.         memcpy(q, f, s1);
  164.     return q;
  165.     }
  166. #endif
  167.